package space

import (
	"src/zeus/entity"
	"src/zeus/iserver"
	"src/zeus/linmath"
	"src/zeus/logger"
	"src/zeus/msgdef"
)

// 发给AOI范围的人，包括自己，但是怪没有自己
func (e *TinyEntity) CastMsgToAllClient(msg msgdef.IMsg) {
	e.CastMsgToAllClientExceptMe(msg)
	// buf, err := entity.PackRaw(msg)
	// if err != nil {
	// 	logger.Errorf("CastMsgToAllClient:%s", err)
	// 	return
	// }
	// if e.GetSpace().EntityCount() <= 1 {
	// 	e.GetSpace().TravsalAOI(e, func(ia iserver.ICoordEntity) bool {
	// 		if ise, ok := ia.(iserver.IEntityStateGetter); ok {
	// 			if ise.GetEntityState() != iserver.Entity_State_Loop {
	// 				return false
	// 			}
	// 			if !ia.ExistsAOIWeather(e) {
	// 				return false
	// 			}
	// 			if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
	// 				ie.CastMsgToMeRaw(buf)
	// 				// ie.CastMsgToMe(msg)
	// 				// ie.Post(iserver.ServerTypeClient, msg)
	// 			}
	// 		}
	// 		return false
	// 	})
	// } else {
	// 	li := make(map[uint64][]uint64)
	// 	e.GetSpace().TravsalAOI(e, func(ia iserver.ICoordEntity) bool {
	// 		if ise, ok := ia.(iserver.IEntityStateGetter); ok {
	// 			if ise.GetEntityState() != iserver.Entity_State_Loop {
	// 				return false
	// 			}
	// 			if !ia.ExistsAOIWeather(e) {
	// 				return false
	// 			}
	// 			if ie, ok := ia.(iserver.IEntity); ok && ia.GetID() != e.GetID() {
	// 				sid, _, _ := ie.GetEntitySrvID(iserver.ServerTypeClient)
	// 				li[sid] = append(li[sid], ie.GetID())
	// 			}
	// 			if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
	// 				ie.CastMsgToMeRaw(buf)
	// 				// ie.CastMsgToMe(msg)
	// 				// ie.Post(iserver.ServerTypeClient, msg)
	// 			}
	// 		}
	// 		return false
	// 	})
	// 	for sid, eids := range li {
	// 		e.GetSpace().BroadCastMsgRaw(sid, iserver.ServerTypeClient, 0,
	// 			buf, eids...)
	// 	}
	// }
	// // logger.Debug(logger.LogKey_Msg, "TinyEntity CastMsgToAllClient ", msg.Name())

}
func (e *TinyEntity) CastMsgToMe(msgdef.IMsg) {

}

// 是帧尾给自己发消息
func (e *TinyEntity) CastMsgToMeRaw(buff []byte) {

}

// 发给AOI范围的人
func (e *TinyEntity) CastMsgToAllClientExceptMe(msg msgdef.IMsg) {
	buf, err := entity.PackRaw(msg)
	if err != nil {
		logger.Errorf("CastMsgToAllClient:%s", err)
		return
	}
	if e.GetSpace().EntityCount() <= 1 {
		e.GetSpace().TravsalAOI(e, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
					ie.CastMsgToMeRaw(buf)
					// ie.CastMsgToMe(msg)
					// ie.Post(iserver.ServerTypeClient, msg)
				}
			}
			return false
		})
	} else {
		li := make(map[uint64][]uint64)
		e.GetSpace().TravsalAOI(e, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IEntity); ok && ia.GetID() != e.GetID() {
					sid, _, _ := ie.GetEntitySrvID(iserver.ServerTypeClient)
					li[sid] = append(li[sid], ie.GetID())
				}
				// if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
				// 	ie.CastMsgToMeRaw(buf)
				// 	// ie.CastMsgToMe(msg)
				// 	// ie.Post(iserver.ServerTypeClient, msg)
				// }
			}
			return false
		})
		for sid, eids := range li {
			e.GetSpace().BroadCastMsgRaw(sid, iserver.ServerTypeClient, 0,
				buf, eids...)
		}
	}
}

// 发给指定范围的人
func (e *TinyEntity) CastMsgToRangeExceptMe(center *linmath.Vector3, radius int, msg msgdef.IMsg) {
	buf, err := entity.PackRaw(msg)
	if err != nil {
		logger.Errorf("CastMsgToAllClient:%s", err)
		return
	}
	if e.GetSpace().EntityCount() <= 1 {
		e.GetSpace().TravsalRange(center, radius, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
					ie.CastMsgToMeRaw(buf)
					// ie.CastMsgToMe(msg)
					// ie.Post(iserver.ServerTypeClient, msg)
				}
			}
			return false
		})
	} else {
		li := make(map[uint64][]uint64)
		e.GetSpace().TravsalRange(center, radius, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IEntity); ok && ia.GetID() != e.GetID() {
					sid, _, _ := ie.GetEntitySrvID(iserver.ServerTypeClient)
					li[sid] = append(li[sid], ie.GetID())
				}
				// if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
				// 	ie.CastMsgToMeRaw(buf)
				// 	// ie.CastMsgToMe(msg)
				// 	// ie.Post(iserver.ServerTypeClient, msg)
				// }
			}
			return false
		})
		for sid, eids := range li {
			e.GetSpace().BroadCastMsgRaw(sid, iserver.ServerTypeClient, 0,
				buf, eids...)
		}
	}
}

// 发给指定范围的人，包括自己 但是怪没有自己
func (e *TinyEntity) CastMsgToRange(center *linmath.Vector3, radius int, msg msgdef.IMsg) {
	e.CastMsgToRangeExceptMe(center, radius, msg)
	// buf, err := entity.PackRaw(msg)
	// if err != nil {
	// 	logger.Errorf("CastMsgToAllClient:%s", err)
	// 	return
	// }
	// if e.GetSpace().EntityCount() <= 1 {
	// 	e.GetSpace().TravsalRange(center, radius, func(ia iserver.ICoordEntity) bool {
	// 		if ise, ok := ia.(iserver.IEntityStateGetter); ok {
	// 			if ise.GetEntityState() != iserver.Entity_State_Loop {
	// 				return false
	// 			}
	// 			if !ia.ExistsAOIWeather(e) {
	// 				return false
	// 			}
	// 			if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
	// 				ie.CastMsgToMeRaw(buf)
	// 				// ie.CastMsgToMe(msg)
	// 				// ie.Post(iserver.ServerTypeClient, msg)
	// 			}
	// 		}
	// 		return false
	// 	})
	// } else {
	// 	li := make(map[uint64][]uint64)
	// 	e.GetSpace().TravsalRange(center, radius, func(ia iserver.ICoordEntity) bool {
	// 		if ise, ok := ia.(iserver.IEntityStateGetter); ok {
	// 			if ise.GetEntityState() != iserver.Entity_State_Loop {
	// 				return false
	// 			}
	// 			if !ia.ExistsAOIWeather(e) {
	// 				return false
	// 			}
	// 			if ie, ok := ia.(iserver.IEntity); ok && ia.GetID() != e.GetID() {
	// 				sid, _, _ := ie.GetEntitySrvID(iserver.ServerTypeClient)
	// 				li[sid] = append(li[sid], ie.GetID())
	// 			}
	// 			if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
	// 				ie.CastMsgToMeRaw(buf)
	// 				// ie.CastMsgToMe(msg)
	// 				// ie.Post(iserver.ServerTypeClient, msg)
	// 			}
	// 		}
	// 		return false
	// 	})
	// 	for sid, eids := range li {
	// 		e.GetSpace().BroadCastMsgRaw(sid, iserver.ServerTypeClient, 0,
	// 			buf, eids...)
	// 	}
	// }
}

func (e *TinyEntity) CastMsgToCenterExceptMe(center *linmath.Vector3, radius int, msg msgdef.IMsg) {
	buf, err := entity.PackRaw(msg)
	if err != nil {
		logger.Errorf("CastMsgToAllClient:%s", err)
		return
	}
	if e.GetSpace().EntityCount() <= 1 {
		e.GetSpace().TravsalCenter(center, radius, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
					ie.CastMsgToMeRaw(buf)
					// ie.CastMsgToMe(msg)
					// ie.Post(iserver.ServerTypeClient, msg)
				}
			}
			return false
		})
	} else {
		li := make(map[uint64][]uint64)
		e.GetSpace().TravsalCenter(center, radius, func(ia iserver.ICoordEntity) bool {
			if ise, ok := ia.(iserver.IEntityStateGetter); ok {
				if ise.GetEntityState() != iserver.Entity_State_Loop {
					return false
				}
				if !ia.ExistsAOIWeather(e) {
					return false
				}
				if ie, ok := ia.(iserver.IEntity); ok && ia.GetID() != e.GetID() {
					sid, _, _ := ie.GetEntitySrvID(iserver.ServerTypeClient)
					li[sid] = append(li[sid], ie.GetID())
				}
				// if ie, ok := ia.(iserver.IClientBroadcaster); ok && ia.GetID() != e.GetID() {
				// 	ie.CastMsgToMeRaw(buf)
				// 	// ie.CastMsgToMe(msg)
				// 	// ie.Post(iserver.ServerTypeClient, msg)
				// }
			}
			return false
		})
		for sid, eids := range li {
			e.GetSpace().BroadCastMsgRaw(sid, iserver.ServerTypeClient, 0,
				buf, eids...)
		}
	}
}
func (e *TinyEntity) CastMsgToCenter(center *linmath.Vector3, radius int, msg msgdef.IMsg) {
	e.CastMsgToCenterExceptMe(center, radius, msg)
}
func (e *TinyEntity) CastRPCToAllClient(methodName string, args ...interface{}) {
}
func (e *TinyEntity) CastRPCToMe(methodName string, args ...interface{}) {

}
func (e *TinyEntity) CastRPCToAllClientExceptMe(methodName string, args ...interface{}) {

}

// 设置在Loop结束的时候，需要广播发消息的方法（主要用于技能有关的RPC消息）
func (e *TinyEntity) SetLoopedMsg(f func()) {
	e.GetSpace().SetLoopedMsg(f)
	// e.LoopSendMsgList = append(e.LoopSendMsgList, f)
}
